package com.zh.freechat.domain.user.repository.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.freechat.common.req.param.UserParam;
import com.zh.freechat.domain.user.entity.ChatUser;
import com.zh.webcommon.common.query.mp.AbstractConditionStrategy;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserConditionStrategy extends AbstractConditionStrategy<ChatUser, UserParam> {

    private static final UserConditionStrategy instance = new UserConditionStrategy();

    public static UserConditionStrategy get() {
        return instance;
    }

    @Override
    protected void specification(QueryWrapper<ChatUser> queryWrapper, UserParam param) {
        var status = param.getStatus();
        var keyword = param.getKeyword();

        queryWrapper
                .eq(status != null, "status", status)
                .eq(StringUtils.hasText(keyword), "username", keyword.trim() + "%");
    }

}
